#include "clientStdafx.h"
#include "SkinSystem.h"

namespace Apo
{

SkinSystem * gSkinSystem = NULL;
//-----------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------
SkinSystem::SkinSystem()
{
	gSkinSystem = this;
	InitBindings();
}
//-----------------------------------------------------------------------------------------
SkinSystem::~SkinSystem()
{
	m_Bindings.clear();
}
//-----------------------------------------------------------------------------------------
void SkinSystem::ParseStringVector(String &str, StringVector &list)
{
// 	list.clear();
// 	StringUtil::trim(str,true,true);
// 	if(str == "") 
// 		return;
// 
// 	int pos = str.find(";");
// 	while(pos != -1)
// 	{
// 		list.push_back(str.substr(0,pos));
// 		str.erase(0,pos + 1);
// 		pos = str.find(";");
// 	}
// 
// 	if(str != "") 
// 		list.push_back(str);
}
//-----------------------------------------------------------------------------------------
void SkinSystem::CleanPath(String &path)
{
// 	std::replace( path.begin(), path.end(), '\\', '/' );
// 
// 	int pos = 0;
// 	pos = path.find("//",0);
// 	while(pos != -1)
// 	{
// 		path.erase(pos,1);
// 		pos = path.find("//",0);
// 	}
// 
// 	pos = path.find("/./",0);
// 	while(pos != -1)
// 	{
// 		path.erase(pos,2);
// 		pos = path.find("/./",0);
// 	}
}
//-----------------------------------------------------------------------------------------
String SkinSystem::ExtractFileName(const String &path)
{
	int pos1 = -1, pos2 = -1;
	String strName = "";
// 	pos1 = path.find_last_of("\\");
// 	pos2 = path.find_last_of("/");
// 
// 	int pos = std::max(pos1,pos2);
// 
// 	if(pos != -1)
// 	{
// 		strName = path.substr(pos + 1,path.size() - (pos + 1));
// 	}

	return strName;
}
//-----------------------------------------------------------------------------------------
String SkinSystem::ExtractFilePath(const String &path)
{
	int pos1 = -1, pos2 = -1;
	String strName = "";
// 	pos1 = path.find_last_of("\\");
// 	pos2 = path.find_last_of("/");
// 
// 	int pos = std::max(pos1,pos2);
// 
// 	if(pos != -1)
// 	{
// 		strName = path.substr(0,pos + 1);
// 	}

	return strName;
}
//-----------------------------------------------------------------------------------------
String SkinSystem::QualifyPath(const String &dirname)
{
	String path = dirname;
// 	if(path.substr(0,1) == ".") path = GetExePath() + "/" + path;
// 
// 	std::replace(path.begin(),path.end(),'\\','/');
// 
// 	// Remember if there is a leading '/'
// 	bool leadingSlash = false;
// 	if(path.substr(0,1) == "/")
// 		leadingSlash = true;
// 
// 	StringVector list;
// 	int pos = path.find("/");
// 	while(pos != -1)
// 	{
// 		if(pos > 0 && path.substr(0,pos) != ".")  // Ignore zero strings and same directory pointers
// 			list.push_back(path.substr(0,pos));
// 		path.erase(0,pos + 1);
// 		pos = path.find("/");
// 	}
// 
// 	if(path != "") 
// 		list.push_back(path);
// 
// 	unsigned int pos2 = 0;
// 	while(list.size() > pos2)
// 	{
// 		if(list[pos2] == "..")
// 		{
// 			list.erase(list.begin() + pos2 - 1,list.begin() + pos2 + 1);
// 			pos2--;
// 		}
// 		else
// 			pos2++;
// 	}
// 
// 	if(list.size() == 0) 
// 		return "";
// 
// 	path = list[0];
// 
// 	for(unsigned int i = 1;i < list.size();i++)
// 	{ 
// 		path += "/" + list[i];
// 	}
// 
// 	if(leadingSlash)
// 		path = "/" + path;

	return path;
}
//-----------------------------------------------------------------------------------------
String SkinSystem::GetRelativePath(const String pathFrom,const String pathTo)
{
	String sFrom = QualifyPath(pathFrom);
	String sTo = QualifyPath(pathTo);

// 	String sFromFirst = sFrom.substr(0,1);
// 	String sToFirst = sTo.substr(0,1);
// 	StringUtil::toLowerCase(sFromFirst);
// 	StringUtil::toLowerCase(sToFirst);
// 
// 	if( sFromFirst != sToFirst) return sTo;
// 
// 	StringVector listfrom;
// 	int pos = sFrom.find("/");
// 	while(pos != -1)
// 	{
// 		listfrom.push_back(sFrom.substr(0,pos));
// 		sFrom.erase(0,pos + 1);
// 		pos = sFrom.find("/");
// 	}
// 
// 	if(sFrom != "") 
// 		listfrom.push_back(sFrom);
// 
// 	StringVector listto;
// 	pos = sTo.find("/");
// 	while(pos != -1)
// 	{
// 		listto.push_back(sTo.substr(0,pos));
// 		sTo.erase(0,pos + 1);
// 		pos = sTo.find("/");
// 	}
// 
// 	if(sTo != "") 
// 		listto.push_back(sTo);
// 
// 	unsigned int length = std::min(listfrom.size(), listto.size());
// 
// 	unsigned int i;
// 	for(i = 0;i < length;i++)
// 	{
// 		String valFrom = listfrom[i];
// 		String valTo = listto[i];
// 		StringUtil::toLowerCase(valFrom);
// 		StringUtil::toLowerCase(valTo);
// 		if(valTo != valFrom) 
// 			break;
// 	}
// 
// 	listfrom.erase(listfrom.begin(),listfrom.begin() + i);
// 	listto.erase(listto.begin(),listto.begin() + i);
// 
// 	if(listfrom.size() == 0 && listto.size() == 0) 
// 		return "./";
// 
 	String strRet = "";
// 	for(i = 0;i < listfrom.size();i++) 
// 		strRet += "../";
// 
// 	if(strRet.length() == 0) 
// 		strRet = "./";
// 
// 	for(i = 0;i < listto.size();i++) 
// 		strRet += listto[i] + "/";
// 
// 	if(listto.size())
// 	{
// 		strRet.erase(strRet.size() - 1,1);
// 	}
	return strRet;
}
//-----------------------------------------------------------------------------------------
unsigned int SkinSystem::GetBindingValue(String arg)
{
// 	if(arg.substr(0,11) == "BIND_CUSTOM" && arg != "BIND_CUSTOMPROPERTY")
// 	{
// 		arg.erase(0,11);
// 		return StringConverter::parseInt(arg) + CUSTOM_BINDING_START;
// 	}
// 
// 	BindingMap::const_iterator it = m_Bindings.find(arg);
// 
// 	if(it != m_Bindings.end())
// 		return it->second;
// 	else
		return BIND_NONE;
}
//-----------------------------------------------------------------------------------------
void SkinSystem::InitBindings()
{
	m_Bindings.clear();
	m_Bindings.insert(btHashString("BIND_MAIN"), BIND_MAIN);
	m_Bindings.insert(btHashString("BIND_TYPE"), BIND_TYPE);
	m_Bindings.insert(btHashString("BIND_NAME"), BIND_NAME);
	m_Bindings.insert(btHashString("BIND_LIGHTTYPE"), BIND_LIGHTTYPE);
	m_Bindings.insert(btHashString("BIND_AMBIENT"), BIND_AMBIENT);
	m_Bindings.insert(btHashString("BIND_FOV"), BIND_FOV);
	m_Bindings.insert(btHashString("BIND_AUTOTRACKTARGET"), BIND_AUTOTRACKTARGET);
	m_Bindings.insert(btHashString("BIND_SKYBOX"), BIND_SKYBOX);
	m_Bindings.insert(btHashString("BIND_SKYBOXACTIVE"), BIND_SKYBOXACTIVE);
	m_Bindings.insert(btHashString("BIND_SKYBOXMATERIAL"), BIND_SKYBOXMATERIAL);
	m_Bindings.insert(btHashString("BIND_SKYBOXDISTANCE"), BIND_SKYBOXDISTANCE);
	m_Bindings.insert(btHashString("BIND_CONFIGFILE"), BIND_CONFIGFILE);
	m_Bindings.insert(btHashString("BIND_SUBENTITIES"), BIND_SUBENTITIES);
	m_Bindings.insert(btHashString("BIND_MESHFILE"), BIND_MESHFILE);
	m_Bindings.insert(btHashString("BIND_CASTSHADOWS"), BIND_CASTSHADOWS); 
	m_Bindings.insert(btHashString("BIND_RECEIVESHADOWS"), BIND_RECEIVESHADOWS);
	m_Bindings.insert(btHashString("BIND_ETLSTAT"), BIND_ETLSTAT);
	m_Bindings.insert(btHashString("BIND_POSITION"), BIND_POSITION);
	m_Bindings.insert(btHashString("BIND_SCALE"), BIND_SCALE);
	m_Bindings.insert(btHashString("BIND_ORIENTATION"), BIND_ORIENTATION	);
	m_Bindings.insert(btHashString("BIND_AABB"), BIND_AABB);
	m_Bindings.insert(btHashString("BIND_DIRECTION"), BIND_DIRECTION);
	m_Bindings.insert(btHashString("BIND_DIFFUSE"), BIND_DIFFUSE);
	m_Bindings.insert(btHashString("BIND_SPECULAR"), BIND_SPECULAR); 
	m_Bindings.insert(btHashString("BIND_POWERSCALE"), BIND_POWERSCALE);
	m_Bindings.insert(btHashString("BIND_SPOTRANGE"), BIND_SPOTRANGE);
	m_Bindings.insert(btHashString("BIND_SPOTRANGEX"), BIND_SPOTRANGEX);
	m_Bindings.insert(btHashString("BIND_SPOTRANGEY"), BIND_SPOTRANGEY);
	m_Bindings.insert(btHashString("BIND_SPOTRANGEZ"), BIND_SPOTRANGEZ);
	m_Bindings.insert(btHashString("BIND_CUSTOMPROPERTY"),BIND_CUSTOMPROPERTY);
}
//-----------------------------------------------------------------------------------------
}